import marsConfig from "./mars-config.js";
import ToolMarksWind from "./plugin/tool-marks-wind.js";
import ToolMarksAnimation from "./plugin/tool-marks-animation.js";
import ToolLoad from "@/tool/ToolLoadJs.js";
import ToolWaitObj from "@/tool/ToolWaitObj.js";
import FactoryPoint from "./factory/factory-point.js";
import FactoryPolyline from "./factory/factory-polyline.js";
import FactoryPolygons from "./factory/factory-polygons.js";
import FactoryDivgraphic from "./factory/factory-divgraphic.js";

import LayerData from "./layer/layer-data.js";
import LayerArea from "./layer/layer-area.js";
import LayerUi from "./layer/layer-ui.js";

import ToolData from "./tool/tool-data.js";
import ToolMap from "./tool/tool-map.js";
import ConfigMarks from "./config-marks.js";
import toolMap from "./tool/tool-map.js";

let baseHeight = 200000;

//引入cesium基础库
// import "mars3d-cesium/Build/Cesium/Widgets/widgets.css";
// import * as Cesium from "mars3d-cesium";

//导入mars3d主库
// import "mars3d/dist/mars3d.css";
// import * as mars3d from "mars3d";

//导入mars3d插件（按需使用，需要先npm install）
// import "mars3d-space";

ToolLoad.loadCssList([
	"https://unpkg.com/mars3d-cesium/Build/Cesium/Widgets/widgets.css",
	"https://unpkg.com/mars3d/dist/mars3d.css",
])

function layer(map, layerObj) {
	if (!layerObj) {
		return map.getLayers({
			basemaps: true, // 是否取config.json中的basempas
			layers: true // 是否取config.json中的layers
		});
	}
	map.addLayer(layerObj);
}

export default {
	async init(dom = "mars3dContainer") {

		await ToolLoad.loadJSList([
			"http://mars3d.cn/lib/Cesium/Cesium.js",
			"http://mars3d.cn/lib/turf/turf.min.js",
			"http://mars3d.cn/lib/mars3d/mars3d.js",
			"http://mars3d.cn/lib/mars3d/plugins/wind/netcdfjs.js",
			"http://mars3d.cn/lib/mars3d/plugins/wind/mars3d-wind.js",
			// "https://unpkg.com/mars3d-space/dist/mars3d-space.js",
		]);

		let map = new mars3d.Map(dom, marsConfig);

		let toolMars = {
			popup(msg = "地图弹窗消息...", position = [114.547015, 34.506371, 1521]) {
				// 传入坐标和内容，可以直接任意弹出
				map.openPopup(position, msg);
				toolMars.setCameraView({
					alt: 17000
				});
			},
			addImg(option) {
				toolMars.layer(new mars3d.layer.ImageLayer(Object.assign({
					url: "//data.mars3d.cn/file/img/zkd-xq.png",
					rectangle: {
						xmin: 117.245648,
						xmax: 117.254431,
						ymin: 31.836891,
						ymax: 31.843413
					},
					zIndex: 20
				}, option)));
				toolMars.setCameraView({
					lat: 31.836891,
					lng: 117.245648,
					alt: 17000
				});
			},
			getCameraView: () => map.getCameraView(),
			layer: (layerObj) => layer(map, layerObj),
			/**
			 * 区域蒙层
			 * @param {Object} option
			 */
			areaMask(option) {
				ToolData.queryChinaMapBorderData().then(data => {
					map.addLayer(new mars3d.layer.GeoJsonLayer(Object.assign({
						name: "遮罩层",
						data,
						mask: {
							xmin: 20.0,
							xmax: 199.0,
							ymin: -40.0,
							ymax: 70.0
						}, // 标识为遮罩层【重点参数】
						symbol: {
							styleOptions: {
								fill: true,
								color: "rgb(2,26,79)",
								opacity: 0.8,
								outline: true,
								// outlineColor: "rgb(206, 230, 253)",
								// outlineWidth: 8,
								// outlineOpacity: 0.8,
								arcType: Cesium.ArcType.GEODESIC,
								clampToGround: true
							}
						}
					}, option)));
				})
			},
			setCameraView(options = {}) {
				map.setCameraView({
					...ConfigMarks.center,
					...options
				})
			},
			/**
			 * 风向图
			 */
			windLayer() {
				ToolMarksWind.init(map);
			},
			addPoint(position) {
				// LayerData.add(FactoryDivgraphic.divgraphic(position))
				// LayerData.add(FactoryPoint.point1(position, {
				// 	// 鼠标移入效果
				// 	highlight: {
				// 		scale: 10
				// 	},
				// 	label: {
				// 		text: "基站名称",
				// 		pixelOffsetY: 10
				// 	}
				// }));
				LayerData.add(FactoryPoint.pointCircleWave(position));
				LayerData.add(FactoryPoint.pointLightCone(position));
			},
			addPolyline(positions) {
				LayerData.add(new mars3d.graphic.PolylineEntity({
					positions,
					style: {
						setHeight: ConfigMarks.baseHeight + 50,
						...FactoryPolyline.polyline1()
					}
				}))
			},
			hideEarth(show = false) {
				toolMap.update({
					globe: {
						show: show
					}
				})
			}
		}
		// toolMars.areaMask();
		// toolMars.windLayer();
		// randomPoints(graphicLayer);

		ToolMarksAnimation.init(map).then(() => {
			console.log("动画执行完成!");
			
			map.basemap = 10000;
			LayerUi.init(map);
			ToolMap.init(map);
			LayerData.init(map);
			
			setTimeout(() => {
				LayerArea.init(map);
				toolMap.update();
				// LayerArea.initArea();
			}, 1000);

			// toolMars.addPoint([114.547015, 34.506371]);
			// LayerData.add(FactoryPoint.pointTetrahedron([113.547015, 35.506371]));
			// LayerData.add(FactoryPoint.routeImg([113.547015, 35.506371]));
			// LayerData.add(FactoryPoint.routeImgWall([112.547015, 36.506371]));
			// LayerData.add(FactoryPoint.pointCircle([110.547015, 38.506371]));
			// LayerData.add(FactoryPoint.pointCircleEntity([108.547015, 38.506371]));
			// LayerData.add(FactoryPoint.pointCylinder([106.547015, 38.506371]));
			

		});
		
		setTimeout(() => {
		}, 2000);
		

		return toolMars;
	}
}

const bbox = [116.984788, 31.625909, 117.484068, 32.021504]

function randomPoints(graphicLayer) {
	graphicLayer.clear()

	const points = turf.randomPoint(100, {
		bbox: bbox
	})

	points.features.forEach((e, index) => {
		const position = e.geometry.coordinates

		const graphic = new mars3d.graphic.BillboardPrimitive({
			position: position,
			style: FactoryPoint.point1(),
			popup: "第" + index + "个"
		})
		graphicLayer.addGraphic(graphic)
	})
};
